package department

import (
	//"fmt"
	"jiabo/appframework/libs/db"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

type departmentBase struct {
	Id                  bson.ObjectId `bson:"_id,omitempty" json:"-"`
	Oid                 string        `bson:"oid" json:"oid"`
	DepartmentName      string        `bson:"departmentName" json:"departmentName"`
	ParentDepartmentOid string        `bson:"parentDepartmentOid" json:"parentDepartmentOid"`
}

//判断数据库中是否存在本Oid的部门
func (department *Department) CheckIfDepartmentExist() (result bool, searchErr error) {
	searchErr = nil
	searchResults := Department{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"departmentbase.oid": department.Oid}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("departments", query)
	}
	err := search()
	if err != nil {
		searchErr = err
	}

	if searchResults.Id != "" {
		result = true
	} else {
		result = false
	}
	return
}
func (department *Department) Save() error {
	//判断如果部门已经存在，就更新，如果不存在就插入。
	ifDepartmentExist, err := department.CheckIfDepartmentExist()
	if err != nil {
		return err
	}
	if ifDepartmentExist {
		update := func(c *mgo.Collection) error {
			fn := c.Update(bson.M{"departmentbase.oid": department.Oid}, department)
			return fn
		}

		updateExec := func() error {
			return db.WithCollection("departments", update)
		}

		err := updateExec()
		return err
	} else {
		insert := func(c *mgo.Collection) error {

			fn := c.Insert(department)
			return fn
		}

		insertExec := func() error {
			return db.WithCollection("departments", insert)
		}

		err := insertExec()
		return err
	}
	return nil
}

func (department *Department) Delete() error {
	//判断如果部门已经存在，就更新，如果不存在就插入。
	ifDepartmentExist, err := department.CheckIfDepartmentExist()
	if err != nil {
		return err
	}
	if ifDepartmentExist {
		delet := func(c *mgo.Collection) error {
			fn := c.Remove(bson.M{"departmentbase.oid": department.Oid})
			return fn
		}

		deleteExec := func() error {
			return db.WithCollection("departments", delet)
		}

		err := deleteExec()
		return err
	}
	return nil
}

func GetDepartmentByOid(oid string) (department Department, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := Department{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"departmentbase.oid": oid}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("departments", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	if searchResults.Id != "" {
		department = searchResults
		return
	}
	return
}

func GetDepartmentByName(depName string) (department Department, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults := Department{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"departmentbase.departmentName": depName}).One(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("departments", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}

	if searchResults.Id != "" {
		department = searchResults
		return
	}
	return
}

func (this *Department) GetParentDepartment() (dep Department, err error) {
	dep, err = GetDepartmentByOid(this.Oid)
	return
}

func (this *Department) GetChildrenDepartment() (searchResults []Department, searchErr error) {
	searchErr = nil
	//department = nil
	searchResults = []Department{}

	query := func(c *mgo.Collection) error {
		fn := c.Find(bson.M{"departmentbase.ParentDepartmentOid": this.Oid}).All(&searchResults)
		return fn
	}
	search := func() error {
		return db.WithCollection("departments", query)
	}
	err := search()
	if err != nil {
		searchErr = err
		return
	}
	return
}
